package com.pan.download4.tool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteChapter {


    private FileWriter fileWriter;

    public WriteChapter(String dirPath,String bookName){
        File dirFile = new File(dirPath);
        if (!dirFile.exists()){
            dirFile.mkdir();
        }
        File bookFile = new File(dirPath+"\\"+bookName+".txt");
        if (bookFile.exists()){
            throw new RuntimeException("已经存在该书本");
        }
        try {
            bookFile.createNewFile();
            this.fileWriter = new FileWriter(bookFile,true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void append(String content){
        try {
            fileWriter.append(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void close(){
        try {
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
